package com.shuorikeji.huohuo.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.android.material.imageview.ShapeableImageView;
import com.shuorikeji.huohuo.R;
import com.shuorikeji.huohuo.bean.MhSdScSSubBean;
import com.shuorikeji.huohuo.callback.ClickCallBack;
import com.shuorikeji.huohuo.utils.BusinessUtils;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;

public class ScMhSdAdapter extends RecyclerView.Adapter<ScMhSdAdapter.BaseViewHolder> {
    private final Context mContext;
    private List<MhSdScSSubBean> mData = new ArrayList<>();
    private ClickCallBack callBack;

    public ScMhSdAdapter(Context context, ClickCallBack callBackFlag) {
        this.mContext = context;
        this.callBack = callBackFlag;
    }

    @Override
    public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.mh_sd_item_layout, parent, false);
        BaseViewHolder holder = new BaseViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(BaseViewHolder holder, @SuppressLint("RecyclerView") int position) {
        holder.ivSc.setImageResource((mData.get(position).getIs_collect() == 0) ? R.mipmap.icon_dj_detail_sc : R.mipmap.icon_dj_detail_ysc);
        holder.ivDz.setImageResource((mData.get(position).getIs_like() == 0) ? R.mipmap.icon_dj_detail_dz : R.mipmap.icon_dj_detail_ydz);
        holder.tvInfo.setText("共" + mData.get(position).getBook_num() + "本  " + mData.get(position).getTags());
        if (mData.get(position).getBooklist() != null) {
            BusinessUtils.setLoadImg(mContext, mData.get(position).getBooklist().getImg(), holder.sivFm);
            holder.tvTitle.setText(mData.get(position).getBooklist().getTitle());
            holder.tvJj.setText(mData.get(position).getBooklist().getContent());
            holder.tvSc.setText(String.valueOf(mData.get(position).getBooklist().getCollect_num()));
            holder.tvDz.setText(String.valueOf(mData.get(position).getBooklist().getLike_num()));
        }
        if (mData.get(position).getUser() != null) {
            BusinessUtils.setLoadImg(mContext, mData.get(position).getUser().getAvatar(), holder.sivCjrTx);
            holder.tvCjr.setText(mData.get(position).getUser().getNickname());
        }
        holder.ivSc.setOnClickListener(view -> {
            //收藏
            callBack.onClick(2, mData.get(position));
        });
        holder.tvSc.setOnClickListener(view -> {
            //收藏
            callBack.onClick(2, mData.get(position));
        });
        holder.ivDz.setOnClickListener(view -> {
            //点赞
            callBack.onClick(3, mData.get(position));
        });
        holder.tvDz.setOnClickListener(view -> {
            //点赞
            callBack.onClick(3, mData.get(position));
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (callBack != null) {
                    callBack.onClick(1, mData.get(position));
                }
            }
        });
    }

    public void setData(List<MhSdScSSubBean> data) {
        if (mData != null) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class BaseViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        ShapeableImageView sivFm;
        TextView tvCjr;
        ShapeableImageView sivCjrTx;
        TextView tvInfo;
        TextView tvJj;
        ImageView ivSc;
        TextView tvSc;
        ImageView ivDz;
        TextView tvDz;

        public BaseViewHolder(View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_title);
            sivFm = itemView.findViewById(R.id.siv_fm);
            tvCjr = itemView.findViewById(R.id.tv_cjr);
            sivCjrTx = itemView.findViewById(R.id.siv_cjr_tx);
            tvInfo = itemView.findViewById(R.id.tv_info);
            tvJj = itemView.findViewById(R.id.tv_jj);
            ivSc = itemView.findViewById(R.id.iv_sc);
            tvSc = itemView.findViewById(R.id.tv_sc);
            ivDz = itemView.findViewById(R.id.iv_dz);
            tvDz = itemView.findViewById(R.id.tv_dz);
        }
    }
}
